#include <iostream>
#include <string>
#include <vector>
#include <unistd.h>
#include <pthread.h>
#include <cstdio>
#include <cstring>

int flag = 100;


void showtid(pthread_t &tid)
{
    printf("tid: 0x%lx\n",tid);
}

std::string FormatID(const pthread_t &tid)
{
    char id[64];
    snprintf(id,sizeof(id),"0x%lx",tid);
    return id;
}
void *routine(void *args)
{
    std::string name = static_cast<const char *>(args);
    pthread_t tid = pthread_self();
    int cnt = 5;
    while (cnt)
    {
        std::cout << "我是一个新线程：我的名字是：" << name << "我的ID是:" << FormatID(tid) << std::endl;
        sleep(1);
        cnt--;
        flag++;
    }
    return (void *)123;
}
int main()
{
    pthread_t tid;
    int n = pthread_create(&tid, nullptr, routine, (void *)"thread-1");
    (void)n;

    showtid(tid);
    int cnt = 5;
    while (cnt)
    {
        std::cout << "我是main线程: 我的名字是: main thread" << " 我的Id是: "
                  << FormatID(pthread_self()) << ", flag: " << flag << std::endl;
        sleep(1);
        cnt--;
    }
    void *ret = nullptr;
    pthread_join(tid, &ret);
    std::cout << "ret is : " << (long long int)ret << std::endl;

    return 0;
}